package com.zyf.school.manager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyf.school.manager.domain.Department;
import com.zyf.school.manager.domain.Staff;
import com.zyf.school.manager.domain.Tree;
import com.zyf.school.manager.service.DepartmentService;
import com.zyf.school.manager.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/staff/")
public class StaffController {

    @Autowired
    StaffService staffService;

    @Autowired
    DepartmentService departmentService;

    Page<Staff> page;

    @RequestMapping("staffManagerAddController")
    public String add(Staff staff,HttpServletRequest request){
        System.out.println("插入数据测试...........");
        staffService.insert(staff);
        page = new Page<>();
        page.setSize(5);
        page.setCurrent(1);
        staffService.page(page);
        request.setAttribute("page",page);
        return "/staff/staffManager";
    }
    @RequestMapping("staffManagerList")
    public String staffManagerList(HttpServletRequest request,@RequestParam(required = false,name = "tel",defaultValue = "15110312981") String tel,@RequestParam("pageIndex")int pageIndex){
        /*List<Staff> list = staffService.getStaffsByTel(tel);*/
        page = new Page<>();
        page.setSize(5);
        page.setCurrent(pageIndex);
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.select("tel",tel);
        page = staffService.page(page);
        request.setAttribute("page",page);
        return "/staff/staffManagerList";
    }

    @RequestMapping("staffManagerQuery")
    public String queryByName(HttpServletRequest request,@RequestParam("pageIndex") int pageIndex){
        /*String name = request.getParameter("name");
        List<Staff> list = staffService.getStaffsByName(name);
        request.setAttribute("list",list);*/
        page = new Page<>();
        page.setSize(5);
        page.setCurrent(pageIndex);
        staffService.page(page);
        request.setAttribute("page",page);

        List<Tree> trees = new ArrayList<>();
        trees = this.setTree(trees,departmentService.queryAll());
        request.setAttribute("tree", JSON.toJSON(trees));
        return "/staff/staffManager";
    }


    public List<Tree> setTree(List<Tree> trees, List<Department> departmentsList){
        for (int i=0; i<departmentsList.size(); i++) {
            Tree tree = new Tree();
            tree.setId(String.valueOf(departmentsList.get(i).getId()));
            tree.setTitle(departmentsList.get(i).getName());
            trees.add(tree);
            if (departmentsList.get(i).getList().size()>0){
                this.setTree(tree.getChildren(),departmentsList.get(i).getList());
            }

        }
        return trees;
    }
}
